﻿#region COPYRIGHT
//--------------------------------------------------------------------------------
// <copyright file="HScrollBar.cs" company="starLiGHT Entertainment Studios">
//    Copyright (c) 2011 
//       Roland Rosenkranz (Glatzemann@email.de)
// </copyright>
// <license>
//   This file is part of starLiGHT.GUI.
//
//   It is released under the Microsoft Public License (Ms-PL).
//
//   You should have received a copy of the Microsoft Public License
//   along with starLiGHT.GUI. If not, see http://sle.codeplex.com/license.
//    
//   ADDITIONAL (commercial) LICENSES for starLiGHT.GUI are available on request.
// </license>
// <version>
// **************[ starLiGHT.Engine SVN ]**********************
// * $Rev::                       $:  Revision of last commit *
// * $Author::                    $:  Author of last commit   *
// * $Date::                      $:  Date of last commit     *
// ************************************************************
// </version>
//--------------------------------------------------------------------------------
#endregion

namespace starLiGHT.GUI.Widgets
{
#region Using Statements
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Graphics;
#endregion

    public class HScrollBar : ScrollBar
    {
        #region Private Members
        private bool knobDragging;
        private float knobPos;
        #endregion

        #region Constructors
        public HScrollBar(GuiManager manager, string name)
            : base(manager, name)
        {
        }

        public HScrollBar(GuiManager manager, string name, string layout)
            : base(manager, name, layout)
        {
        }
        #endregion

        public RectangleF KnobRectangle
        {
            get
            {
                Image knob = Layout.Skin.GetImageReference("HScrollBar.Knob");
                float knobWidth = knob.Rectangle.Width * 0.5f;
                float left = ClientRectangle.Left + 9f;
                float right = ClientRectangle.Right - 9f;
                float relativeVal = (float)Value / ((float)Maximum - (float)Minimum);
                float knobRelativePos = MathHelper.Lerp(left, right, relativeVal) - knobWidth;

                return new RectangleF(knobRelativePos, ClientRectangle.Top, knob.Rectangle.Width, knob.Rectangle.Height);
            }
        }

        public override bool CanFocus
        {
            get
            {
                return true;
            }
        }

        public override void Draw(GameTime gameTime, IGuiRenderer renderer)
        {
            base.Draw(gameTime, renderer);

            Image knob = Layout.Skin.GetImageReference("HScrollBar.Knob");
            RectangleF knobRect = this.KnobRectangle;
            renderer.DrawSprite(knob.Texture, 1f, knobRect.X, knobRect.Top, knobRect.Width, knobRect.Height, knob.Rectangle.Left, knob.Rectangle.Top, knob.Rectangle.Width, knob.Rectangle.Height, Tint);
        }

        public override void OnMouseDown(object sender, MouseEventArgs e)
        {
            if (e.LeftButton)
            {
                RectangleF knobRect = this.KnobRectangle;

                if (knobRect.Contains(e.Position))
                {
                    this.knobDragging = true;
                    this.knobPos = e.Position.X;
                    ////System.Diagnostics.Trace.WriteLine("HScrollBar.Knob.MouseDown");
                    this.UpdateValue(this.knobPos);
                }
                else if (e.WidgetUnderMouse)
                {
                    if (e.Position.X < this.knobPos)
                    {
                        this.Value -= LargeChange;
                    }
                    else
                    {
                        this.Value += LargeChange;
                    }

                    this.UpdateKnobPos();
                }
            }

            base.OnMouseDown(sender, e);
        }

        public override void OnMouseMove(object sender, MouseEventArgs e)
        {
            base.OnMouseMove(sender, e);

            if (this.knobDragging)
            {
                this.UpdateValue(e.Position.X);
                this.UpdateKnobPos();
            }
        }

        public override void OnMouseUp(object sender, MouseEventArgs e)
        {
            base.OnMouseUp(sender, e);

            this.knobDragging = false;
        }

        private void UpdateValue(float positionX)
        {
            float r = positionX - ClientRectangle.Left;
            float w = ClientRectangle.Right - ClientRectangle.Left;
            float p = r / w;
            int rp = (int)(p * (Maximum - Minimum));
            Value = rp;
        }

        private void UpdateKnobPos()
        {
            float w = ClientRectangle.Right - ClientRectangle.Left;
            float d = Maximum - Minimum;

            this.knobPos = ((Value / d) * w) + ClientRectangle.Left;
        }
    }
}
